using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;

namespace MaskData
{
	public class MaskNodes
	{
		private List<MaskNode> _nodes = new List<MaskNode>();
		private readonly Dictionary<String, ElementType> _types;
		private DataTable _table;
		private String _KeyField;
		private String _MaskNodeID;
		public String MaskNodeID
		{
			get {
				return _MaskNodeID;
			}
		}
		public ElementType KeyType
		{
			get {
				return _types[_KeyField];
			}
		}
		public MaskNode GetNodesByKey(String keyValue)
		{
			MaskNode resultNode = null;
			foreach (MaskNode node in _nodes)
			{
				MaskElement element = node.GetFieldElement(_KeyField);
				if (element.Type.IsKey)
				{
					StringMaskElement KeyElement = element as StringMaskElement;
					Debug.Assert(KeyElement != null);
					if (KeyElement.Value == keyValue)
					{ 
						resultNode = node;
						break;
					}
				}
			}
			Debug.Assert(resultNode != null);
			return resultNode;
		}
		public Dictionary<String, ElementType> Types
		{
			get {
				return _types;
			}
		}
		public List<ElementType> ListTypes
		{
			get {
				List<ElementType> types = new List<ElementType>();
				foreach (KeyValuePair<String, ElementType> typePair in _types)
				{
					types.Add(typePair.Value);
				}
				return types;
			}
		}
		public MaskNodes(ITypesReader reader, String maskNodeId)
		{
			_MaskNodeID = maskNodeId;
			_types = new Dictionary<string, ElementType>();

			bool isOneKey = false;
			foreach (ElementType type in reader.GetNodeTypesByID(maskNodeId))
			{
				_types[type.FieldName] = type;
				if (type.IsKey)
				{
					Debug.Assert(!isOneKey);
					_KeyField = type.FieldName;
					isOneKey = true;
				}
			}
			_table = CreateTable();
		}

		public MaskNode NewNode()
		{
			MaskNode newNode = new MaskNode(_types);
			return newNode;
		}

		public void Add(MaskNode node)
		{
			_nodes.Add(node);
		}
		public DataTable GetDataTable()
		{
			_table.Rows.Clear();

			foreach (MaskNode node in _nodes)
			{
				DataRow row = _table.NewRow();
				node.SaveToRow(row);
				_table.Rows.Add(row);
			}
			return _table;
		}
		/// <summary>
		/// Create Table
		/// </summary>
		/// <returns></returns>
		///
		private DataTable CreateTable()
		{
			DataTable table = new DataTable("myMaskNodes");
			if (_types.Count == 0)
				return table;
			List<DataColumn> keyColumns = new List<DataColumn>();
			//create DataColumn
			foreach (KeyValuePair<String, ElementType> typePair in _types)
			{
				DataColumn column = typePair.Value.ToColumn();
				table.Columns.Add(column);
				if (typePair.Value.IsKey)
					keyColumns.Add(column);
			}

			//set the Primary key of DataTable
			Debug.Assert(keyColumns.Count != 0);
			DataColumn[] PrimaryKeyColumns = new DataColumn[keyColumns.Count];

			//if the count of keys is one, set the Unique of this column to true;
			if (keyColumns.Count == 1)
			{
				keyColumns[0].Unique = true;
			}
			table.PrimaryKey = PrimaryKeyColumns;

			return table;
		}

	}
}
